capture log close
log using replica_fig4__enrista17.log,replace

*       REPLICA_FIG4__ENRISTA17.LOG

*       FIRST VERSION   JUNE 20, 2006
*       THIS VERSION    AUGUST 10, 2012

*       LAST REVISOR    AI

*       INPUT FILE:             BASE12_6.DTA
*       OUTPUT FILE:            REPLICA FOR FIGURE 4
*


version 9.0

di "log file printed on $S_DATE at $S_TIME"


set more 1
clear
program drop _all
macro drop _all
scalar drop _all

# delimit ;

set scheme s1color;
set memory 300000; 
set matsize 250;
tempfile tmpf1 tmpf2 tmpf3 tmpf4;

use base12_6, clear;

* KEEP MEDIA PRIMO ANNO;
sort id anac;
qui by id: gen gpa_1 = gpa[1]; 

* lagged income and taxes;
gen ym1 = l1.y;
gen ydm1 = l1.yd;
gen tteom1 = l1.tteo;
gen tpaidm1 = l1.tpaid;

gen ym2 = l2.y;
gen ydm2 = l2.yd;
gen tteom2 = l2.tteo;
gen tpaidm2 = l2.tpaid;

gen ym3 = l3.y;
gen ydm3 = l3.yd;
gen tteom3 = l3.tteo;
gen tpaidm3 = l3.tpaid;



* SELEZIONE DEL CAMPIONE;
* keep studenti in corso regolare al 4 anno;
keep if real_status==4&aniscr==4&posizione==14 ;

xtdes;

*******NEW
* restrizione della distanza dalla soglia;
keep if yd>-3&yd<3;

xtdes;





* VARIABILI NECESSARIE;

tab anac, gen(daa);

gen y2 = y*y;
gen y3 = y*y2;
gen y4 = y*y3;

label var y "Real income - thousands of Euros, base 2000";

tab fascia_teo, gen(dfa);

gen yd2 = yd*yd;
gen yd3 = yd2*yd;
gen yd4 = yd3*yd;

label var yd "Distance from nearest discontinuity in 1000 euros";


* ALTRE VARIABILI NECESSARIE PER ANALISI  RDD;


*generate for compliance analysis;
gen comp = tpaid>=tteo1;
label var comp "fraction with tpaid >tteo1";

* generate dummies for nearest discontinuity;
tab neardisc , gen(dnear);

* calcola gli scarti dalla media di anno/punto di discontinuit�

sort anac neardisc;
egen mtp=mean(tpaid), by(anac neardisc);
gen wtp=tpaid-mtp;
label var wtp "paid tuition in deviation from mean";



* VARIABILI PRE INTERVENTION;

* Normalizzazione del voto di matematica;
gen normat = (mat - 0)/(31-0);
sum normat votodip;
corr normat votodip;

* Normalizzazione del voto di statistica;
gen norsta = (sta - 0)/(31-0);
sum norsta votodip;
corr norsta votodip;

* Normalizzazione del gpa 1 anno;
gen norgpa1 = (gpa_1 - 0)/(31-0);
sum norgpa1 votodip;
corr norgpa1 votodip;

* generate dummy liceo from  tipo diploma;
gen typedip =  cod_tit_dipl ==1 ;


* altre variabili da usare nelle regressioni

outmil
female
votodip

;



# delimit cr




* FIGURE 4


forvalues i=2(5)7 {

* grafico voto diploma
lowess votodip yd if neardisc==`i', generate(votodipsm`i') by(z) nograph mean
label var votodipsm`i' "Smooth average"
sort neardisc yd
twoway line votodipsm`i' yd if neardisc==`i' ,ysc(r(0.7 0.9)) ylabel(0.7(0.05)0.9) xline(0) title("Highschool grade: disc= `i'") saving(votodipsm`i', replace) 
graph export votodipsm`i'.eps, replace logo(off)


* grafico su income lumping

lowess ym3 yd if neardisc==`i', generate(ym3sm`i') by(z) nograph mean
label var ym3sm`i' "Smooth average"
sort neardisc yd
sum ym3sm`i' if  neardisc==`i'
local min = round(r(mean),10) - 10
di "this is the min of ym3 for disc `i'  " `min' 
local max = round(r(mean),10) + 10
di "this is the max of ym3for disc `i'  " `max' 
twoway line ym3sm`i' yd if neardisc==`i' , ysc(r(`min' `max'))  ylabel(`min'(10)`max') xline(0) title("Pre-Bocconi income: disc=`i'" ) saving(ym3sm`i', replace) 
graph export ym3sm`i'.eps, replace logo(off)

}


* grafici finali


* figura a 4 pannelli sorting e votodip
*gr combine ym3sm2.gph  votodipsm2.gph ym3sm7.gph  votodipsm7.gph ,  saving(sort_cont, replace)
*graph export fin_sort_cont.eps, replace logo(off)

gr combine ym3sm2.gph  votodipsm2.gph ym3sm7.gph  votodipsm7.gph ,  saving(fig_4, replace)
graph export fig_4.eps, replace logo(off)








log close






